#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  int fd;
  size_t len;
  void *addr;

  if (argc != 3 || strcmp(argv[1], "--help") == 0)
    printf("usage: %s shm-name string\n", argv[0]);
  fd = shm_open(argv[1], O_RDWR, 0);
  if (fd == -1)
    printf("Failed to open share memory %s\n",argv[1]);
  len = strlen(argv[2]);
  if (ftruncate(fd,len) == -1)
    printf("failed to resize shared memory %s\n",argv[1]);
  addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED)
    printf("Failed to mmap\n");
  if (close(fd) == -1)
    printf("Failed to close fd\n");
  printf("copying  %ld bytes\n", (long)len);
  memcpy(addr, argv[2], len);
  return 0;
}
